/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Table;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.Basic;
import javax.persistence.ManyToMany;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Julian Peña
 * @version 1.0
 */
@Entity
@Table(name = "permission")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Permission.findAll", query = "SELECT p FROM Permission p"),
    @NamedQuery(name = "Permission.findById", query = "SELECT p FROM Permission p WHERE p.id = :id"),
    @NamedQuery(name = "Permission.findByName", query = "SELECT p FROM Permission p WHERE p.name = :name") })
public class Permission implements Serializable {
    /**Version of the Serializable interface.
     */
    private static final long serialVersionUID = 1L;
    /**Id from the Permission.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    /**Name form the Permission.
     */
    @Basic(optional = false)
    @Column(name = "name")
    private String name;
    /**List of roles with the Permission.
     */
    @JoinTable(name = "rol_permission", joinColumns = {
        @JoinColumn(name = "permission_id", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "rol2_id", referencedColumnName = "id")})
    @ManyToMany
    private List<Rol> rolList;

    /**Default constructor.
     */
    public Permission() {
    }

    /**Constructor using the Id.
     * @param newId The new value
     */
    public Permission(final Long newId) {
        this.id = newId;
    }

    /**Constructor using the Id and the Name.
     * @param newId The new value
     * @param newName The nea value
     */
    public Permission(final Long newId, final String newName) {
        this.id = newId;
        this.name = newName;
    }

    /**Getter for Id.
     * @return The Id value
     */
    public Long getId() {
        return id;
    }

    /**Setter for Id.
     * @param newId The new value
     */
    public void setId(final Long newId) {
        this.id = newId;
    }

    /**Getter forn Name.
     * @return The name value
     */
    public String getName() {
        return name;
    }

    /**Setter for Name.
     * @param newName The new value
     */
    public void setName(final String newName) {
        this.name = newName;
    }

    /**Getter for the Roles List.
     * @return The RoleList.
     */
    @XmlTransient
    public List<Rol> getRolList() {
        return rolList;
    }

    /**Setter fot the Roles List.
     * @param newRolList The new RolList.
     */
    public void setRolList(final List<Rol> newRolList) {
        this.rolList = newRolList;
    }

    /**Assignment of the hashcode.
     * @return The hash
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }
    
    /**Compare two Permissions objects based on the Id.
     * @param object The object to compare
     * @return The result of the comparison.
     */
    @Override
    public boolean equals(final Object object) {
        if (!(object instanceof Permission)) {
            return false;
        }
        Permission other = (Permission) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    /**Convert to string using the Id.
     * @return The string
     */
    @Override
    public String toString() {
        return "org.colswe.solestac.entity.Permission[ id=" + id + " ]";
    }
    
}
